package com.socialuni.center.web.user.userManage;

import com.socialuni.center.web.common.ContentCheck;
import com.socialuni.center.web.model.DO.user.UserDetailDO;
import com.socialuni.sdk.exception.SocialBusinessException;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.model.UserEditVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class CenterUserCheck {

    @Resource
    private ContentCheck contentCheck;

    public void checkUserNickname(String nickname, UserDO mineUser) {
        if (StringUtils.isEmpty(nickname)) {
            throw new SocialBusinessException("昵称不能为空");
        }
        if (nickname.length() > 6) {
            throw new SocialBusinessException("昵称长度不能大于6");
        }
        try {
            contentCheck.checkShortContent(nickname);
        } catch (SocialBusinessException e) {
            throw new SocialBusinessException("昵称疑似包含违规内容，禁止修改，请修改后重试");
        }
        mineUser.setNickname(StringUtils.substring(nickname, 0, 6));
    }

    public void checkUserCity(String city, UserDO mineUser) {
        //保存地区名
        if (StringUtils.isNotEmpty(city)) {
            if (city.length() > 10) {
                throw new SocialBusinessException("市县区名称长度不能大于10");
            } else {
                try {
                    contentCheck.checkShortContent(city);
                } catch (SocialBusinessException e) {
                    throw new SocialBusinessException("地区名称疑似包含违规内容，禁止修改，请修改后重试");
                }
                mineUser.setCity(city);
            }
        }
    }

    public void checkUserContactAccount(UserEditVO userEditVO, UserDetailDO userDetailDO) {
        //保存用户详情相关，联系方式
        String contactAccount = userEditVO.getContactAccount();
        String wxAccount = userEditVO.getWxAccount();
        String qqAccount = userEditVO.getQqAccount();
        String wbAccount = userEditVO.getWbAccount();

        //代表老版本
        if (contactAccount == null) {
            if (wxAccount != null) {
                if (StringUtils.isNotEmpty(wxAccount)) {
                    if (wxAccount.length() > 23) {
                        throw new SocialBusinessException("微信账户名不得超过23个字符，例如：491369310");
                    } else if (wxAccount.length() < 6) {
                        throw new SocialBusinessException("微信账户名必须大于5个字符，例如：491369310");
                    } else {
                        try {
                            contentCheck.checkShortContent(wxAccount);
                        } catch (SocialBusinessException e) {
                            throw new SocialBusinessException("微信联系方式疑似包含违规内容，请修改后重试");
                        }
                    }
                }
                userDetailDO.setWxAccount(wxAccount);
            }
            if (qqAccount != null) {
                if (StringUtils.isNotEmpty(qqAccount)) {
                    if (qqAccount.length() > 15) {
                        throw new SocialBusinessException("qq号不得超过15个字符，例如：491369310");
                    } else if (qqAccount.length() < 5) {
                        throw new SocialBusinessException("qq号必须大于4个字符，例如：491369310");
                    } else {
                        try {
                            contentCheck.checkShortContent(qqAccount);
                        } catch (SocialBusinessException e) {
                            throw new SocialBusinessException("qq联系方式疑似包含违规内容，请修改后重试");
                        }
                    }
                    userDetailDO.setQqAccount(qqAccount);
                }
            }
            if (wbAccount != null) {
                if (StringUtils.isNotEmpty(wbAccount)) {
                    if (wbAccount.length() > 20) {
                        throw new SocialBusinessException("微博名称不得超过20个字符，例如：清池");
                    } else if (wbAccount.length() < 2) {
                        throw new SocialBusinessException("微博名称必须大于1个字符，例如：清池");
                    } else {
                        try {
                            contentCheck.checkShortContent(wbAccount);
                        } catch (SocialBusinessException e) {
                            throw new SocialBusinessException("微博名称疑似包含违规内容，请修改后重试");
                        }
                    }
                }
                userDetailDO.setWbAccount(wbAccount);
            }
            //老版本将其他的联系方式放置到联系方式上
            //如果微信不为空，且长度大于5
            if (StringUtils.isNotEmpty(wxAccount)) {
                contactAccount = "wx:" + wxAccount;
            } else if (StringUtils.isNotEmpty(qqAccount)) {
                contactAccount = "qq:" + qqAccount;
            } else if (StringUtils.isNotEmpty(wbAccount)) {
                contactAccount = "wb:" + wbAccount;
            } else {
                contactAccount = "";
            }
        } else {
            //新版本
            if (StringUtils.isNotEmpty(contactAccount)) {
                if (contactAccount.length() > 30) {
                    throw new SocialBusinessException("联系方式不能超过30个字符，例如：vx:491369310");
                } else if (contactAccount.length() < 5) {
                    throw new SocialBusinessException("联系方式必须大于4个字符，例如：vx:491369310");
                } else {
                    try {
                        contentCheck.checkShortContent(wbAccount);
                    } catch (SocialBusinessException e) {
                        throw new SocialBusinessException("联系方式疑似包含违规内容，请修改后重试");
                    }
                }
            }
        }
        userDetailDO.setContactAccount(contactAccount);
    }
}
